// 格式化时间
const formatTime = date => {
  const year = date.getFullYear();
  const month = date.getMonth() + 1;
  const day = date.getDate();
  const hour = date.getHours();
  const minute = date.getMinutes();
  const second = date.getSeconds();
  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':');
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n 
}

const formatTimeTwo = (number, format)=> {

  var formateArr = ['Y', 'M', 'D', 'h', 'm', 's'];
  var returnArr = [];

  var date = new Date(number * 1000);
  returnArr.push(date.getFullYear());
  returnArr.push(formatNumber(date.getMonth() + 1));
  returnArr.push(formatNumber(date.getDate()));

  returnArr.push(formatNumber(date.getHours()));
  returnArr.push(formatNumber(date.getMinutes()));
  returnArr.push(formatNumber(date.getSeconds()));

  for (var i in returnArr) {
      format = format.replace(formateArr[i], returnArr[i]);
  }
  return format;
}
//获取url参数
const getUrlParam = (paraName, url) => {
  var arrObj = url.split("?");
  if (arrObj.length > 1) {
    var arrPara = arrObj[1].split("&");
    var arr;
    for (var i = 0; i < arrPara.length; i++) {
      arr = arrPara[i].split("=");
      if (arr != null && arr[0] == paraName) {
        return arr[1];
      }
    }
    return "";
  }
  else {
    return "";
  }
}

// urlencode参数
const parseParam = (json) => {
  if(!json) return '';
  let tmps = [];
  for (var key in json) {
    tmps.push(key + '=' + encodeURIComponent(json[key]));
  }
  return tmps.join('&');
};
// 时间格式化 返回YYYY-MM-DD hh-mm-ss
 const timeFormat = (time, type = 'YYYY-MM-DD hh-mm-ss', line = "-") => {
	if (!time) return ''
	let date = new Date((time.replace && time.replace(/-/g, "/")) || time);
	let Y = date.getFullYear() + line;
	let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + line;
	let D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate());
	let h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
	let m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
	let s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
	if (type == 'YYYY-MM-DD hh-mm-ss') {
		return Y + M + D + ' ' + h + m + s
	}
	if (type == 'YYYY-MM-DD') {
		return Y + M + D
	} else {
		return h + m + s
	}
}
// 合并日期 
const format =(value)=> {
  var date = new Date(parseInt(value) * 1000);
  var year = date.getFullYear();
  var month = date.getMonth() + 1;
  var day = date.getDate();
  return [year, month, day].join('-');
}

/**
 * 时间戳差 转为 剩余时间
 */
function remainTime(diffms) {
  const daysms = 24 * 60 * 60 * 1000;
  const hoursms = 60 * 60 * 1000;
  const minutems = 60 * 1000;
  const secondms = 1000;
  var result = '';
  var hasfrontVal = false;

  var differDay = Math.floor(diffms / daysms);
  result = differDay ? differDay + '天' : '';
  hasfrontVal = differDay ? true : false;

  diffms -= differDay * daysms;
  var differHour = Math.floor(diffms / hoursms);
  result += differHour ? differHour + '时' : (hasfrontVal ? '0时' : '');
  hasfrontVal = differHour ? true : false;

  diffms -= differHour * hoursms;
  var differMinute = Math.floor(diffms / minutems);
  result += differMinute ? differMinute + '分' : (hasfrontVal ? '0分' : '');

  diffms -= differMinute * minutems;
  var differSecond = Math.floor(diffms / secondms);
  result += differSecond ? differSecond + '秒' : '0秒';

  return result;
}

/**
 * 此处2为保留两位小数
 */
function toFix(value) {
  if(!value)return '';
  return value.toFixed(2)
}
export default {
  parseParam,
  getUrlParam,
  formatTime,
  format,
  remainTime,
  toFix,
  timeFormat,
  formatTimeTwo
}
